package com.youmu.sichuansfc.filter;

import com.youmu.sichuansfc.filter.FundManager.FundManagerTokenUserHandlerMethodArgumentResolver;
import com.youmu.sichuansfc.filter.FundManager.LoginInterceptor;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.youmu.sichuansfc")
@PropertySource(value = "classpath:application.properties",  ignoreResourceNotFound = true,encoding = "UTF-8")
@Service
public class MvcConfig extends WebMvcConfigurerAdapter {
    private static final Logger logger = Logger.getLogger(MvcConfig.class);

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);

        // 注册监控拦截器
//        registry.addInterceptor(new DevLoggerInterceptor())
//                .addPathPatterns("/*");
                // .excludePathPatterns("/**.html");  //排除地址
        // 基金报送平台
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**/*.bs");
                //.addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    /**
     *  自定义参数解析器
     */
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)  {
        super.addArgumentResolvers(argumentResolvers);
        // 基金报送平台自定义参数解析
        argumentResolvers.add(new FundManagerTokenUserHandlerMethodArgumentResolver());
    }

    /**
     *  静态资源访问
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
    }
}